﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Data.Common;
using DataAccessor.Exceptions;

namespace DataAccessor
{
    /// <summary>
    /// 异步执行委托
    /// </summary>
    /// <typeparam name="TResult">委托的返回结果类型</typeparam>
    /// <param name="statementName">T-SQL语句块名称</param>
    /// <param name="paramValueDict">T-SQL语句参数字典</param>
    /// <returns>委托的返回结果</returns>
    internal delegate TResult AsyncExecuteDelegate<TResult>(string statementName, Dictionary<string, object> paramValueDict);

    /// <summary>
    /// 异步执行委托
    /// </summary>
    /// <typeparam name="TResult">委托的返回结果类型</typeparam>
    /// <param name="statementName">T-SQL语句块名称</param>
    /// <param name="paramValueDict">T-SQL语句参数字典</param>
    /// <returns>委托的返回结果</returns>
    internal delegate TResult AsyncExecuteCommandDelegate<TResult>(CommandType commandType, string commandText, params DbParameter[] commandParameters);

    /// <summary>
    /// 异步执行完成事件的句柄
    /// </summary>
    /// <typeparam name="TReturn">事件的返回结果类型</typeparam>
    /// <param name="result">事件的返回结果</param>
    /// <param name="accessor">数据访问器的实例</param>
    public delegate void AsyncExecuteCompletedEventHandler<TReturn>(TReturn result, IDataAccessor accessor);
    /// <summary>
    /// 执行失败事件的句柄
    /// </summary>
    /// <param name="exception">异常</param>
    /// <param name="accessor">数据访问器的实例</param>
    public delegate void ExecuteFailedEventHandler(CommandExecuteFailedException exception, IDataAccessor accessor);

    /// <summary>
    /// 数据访问接口
    /// </summary>
    public abstract partial class IDataAccessor
    {
        /// <summary>
        /// AsyncExecuteNonQuery成功事件
        /// </summary>
        public event AsyncExecuteCompletedEventHandler<int> AsyncExecuteNonQueryCompleted;
        /// <summary>
        /// 当AsyncExecuteNonQuery成功事件
        /// </summary>
        /// <param name="result">，如果执行失败且不重新抛出异常，则为-1</param>
        /// <param name="dataAccessor">数据访问的实现</param>
        protected void OnAsyncExecuteNonQueryCompleted(int result, IDataAccessor dataAccessor)
        {
            if (AsyncExecuteNonQueryCompleted != null)
            {
                AsyncExecuteNonQueryCompleted(result, dataAccessor);
            }
        }

        /// <summary>
        /// AsyncExecuteReader成功事件
        /// </summary>
        public event AsyncExecuteCompletedEventHandler<IDataReader> AsyncExecuteReaderCompleted;
        /// <summary>
        /// 当AsyncExecuteReader成功事件
        /// </summary>
        /// <param name="result">，如果执行失败且不重新抛出异常，则为null</param>
        /// <param name="dataAccessor">数据访问的实现</param>
        protected void OnAsyncExecuteReaderCompleted(IDataReader result, IDataAccessor dataAccessor)
        {
            if (AsyncExecuteReaderCompleted != null)
            {
                AsyncExecuteReaderCompleted(result, dataAccessor);
            }
        }

        /// <summary>
        /// AsyncExecuteScalar成功事件
        /// </summary>
        public event AsyncExecuteCompletedEventHandler<object> AsyncExecuteScalarCompleted;
        /// <summary>
        /// 当AsyncExecuteScalar成功事件
        /// </summary>
        /// <param name="result">，如果执行失败且不重新抛出异常，则为null</param>
        /// <param name="dataAccessor">数据访问的实现</param>
        protected void OnAsyncExecuteScalarComplated(object result, IDataAccessor dataAccessor)
        {
            if (AsyncExecuteScalarCompleted != null)
            {
                AsyncExecuteScalarCompleted(result, dataAccessor);
            }
        }

        /// <summary>
        /// AsyncExecuteDataSet成功事件
        /// </summary>
        public event AsyncExecuteCompletedEventHandler<DataSet> AsyncExecuteDataSetCompleted;
        /// <summary>
        /// 当AsyncExecuteDataSet成功事件
        /// </summary>
        /// <param name="result">执行结果，如果执行失败且不重新抛出异常，则为null</param>
        /// <param name="dataAccessor">数据访问的实现</param>
        protected void OnAsyncExecuteDataSetComplated(DataSet result, IDataAccessor dataAccessor)
        {
            if (AsyncExecuteDataSetCompleted != null)
            {
                AsyncExecuteDataSetCompleted(result, dataAccessor);
            }
        }

        /// <summary>
        /// 异步执行命令失败事件，只对DbException有效。
        /// </summary>
        public event ExecuteFailedEventHandler ExecuteFailed;
        /// <summary>
        /// 异步执行命令失败事件
        /// </summary>
        /// <param name="exception">异常</param>
        /// <param name="dataAccessor">数据访问的实现</param>
        protected void OnExecuteFailed(CommandExecuteFailedException exception, IDataAccessor dataAccessor)
        {
            if (ExecuteFailed != null)
            {
                // 记录执行失败的日志

                // 触发执行失败事件
                ExecuteFailed(exception, dataAccessor);
            }
        }
    }
}
